package com.neu.cloudFactory.controller;

import com.neu.cloudFactory.DAO.EquipMsgDAO;
import com.neu.cloudFactory.PO.EquipMsg;
import com.neu.cloudFactory.PO.EquipType;
import com.neu.cloudFactory.PO.User;
import com.neu.cloudFactory.VO.EquipMsgVO;
import com.neu.cloudFactory.VO.EquipTypeVO;
import com.neu.cloudFactory.service.EquipService;
import com.neu.cloudFactory.service.FactoryService;
import com.neu.cloudFactory.service.UserService;
import com.neu.cloudFactory.utils.result.Result;
import com.neu.cloudFactory.utils.result.ResultFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/equip")
public class EquipController {
    final EquipService equipService;
    final FactoryService factoryService;
    final UserService userService;

    public EquipController(EquipService equipService, FactoryService factoryService, UserService userService) {
        this.equipService = equipService;
        this.factoryService = factoryService;
        this.userService = userService;
    }

    //列出对应设备列表
    @CrossOrigin
    @PostMapping(value = "/findAllEquip")
    @ResponseBody
    public Result findAllEquip(@RequestBody User user){
        System.out.println(user);
        User user1 = userService.findByAccount(user.getAccount());
        System.out.println(user1);
        if(user1.getCharacter().equals("超级管理员")){
            return ResultFactory.buildSuccessResult(equipService.findAllEquipVO());
        }else{
            return ResultFactory.buildSuccessResult(equipService.findAllEquipVO(
                    factoryService.findFactoryByUserId(user1.getId()).getId()));
        }
    }
    //列出所有设备类型
    @CrossOrigin
    @GetMapping(value = "/findAllType")
    @ResponseBody
    public Result findAllType(){
        return ResultFactory.buildSuccessResult(equipService.findAllEquipTypeVO());
    }

    //添加设备
    @CrossOrigin
    @PostMapping(value = "/addEquip")
    @ResponseBody
    public Result addEquip(@RequestBody EquipMsgVO requestEquipMsgVO){
        EquipMsg equipMsg = equipService.changeType(requestEquipMsgVO);
        equipMsg.setCode(equipService.generateCode());
        if(equipService.isExist(equipMsg)){
            return ResultFactory.buildFailResult("已存在该设备");
        }else{
            equipService.saveEquip(equipMsg);
            return ResultFactory.buildSuccessResult(equipService.addIdToEquip(equipMsg,requestEquipMsgVO.getUserAccount()),"添加成功");
        }
    }

    //添加设备类型
    @CrossOrigin
    @PostMapping(value = "/addEquipType")
    @ResponseBody
    public Result addEquipType(@RequestBody EquipType requestEquipType){
        if(null!=equipService.findEquipTypeById(requestEquipType.getId())){
            return ResultFactory.buildFailResult("已存在该设备种类");
        }else{
            equipService.saveEquipType(requestEquipType);
            EquipTypeVO equipTypeVO = new EquipTypeVO(requestEquipType);
            return ResultFactory.buildSuccessResult(equipTypeVO,"添加成功");
        }
    }
    //修改设备
    @CrossOrigin
    @PostMapping(value = "/modifyEquip")
    @ResponseBody
    public Result modifyEquip(@RequestBody  EquipMsgVO requestEquipMsgVO){
        EquipMsg equipMsg = equipService.changeType(requestEquipMsgVO);
        //修改的时候需要传入id
        equipMsg.setId(Integer.parseInt(requestEquipMsgVO.getId()));
        equipService.saveEquip(equipMsg);
        return ResultFactory.buildSuccessResult("修改成功");
    }
    //修改设备类型
    @CrossOrigin
    @PostMapping(value = "/modifyEquipType")
    @ResponseBody
    public Result modifyEquipType(@RequestBody EquipType requestEquipType){
        equipService.saveEquipType(requestEquipType);
        return ResultFactory.buildSuccessResult("修改成功");
    }
    //删除设备
    @CrossOrigin
    @PostMapping(value = "/deleteEquipMsg")
    @ResponseBody
    public Result deleteEquipMsg(@RequestBody EquipMsgVO requestEquipMsgVO){
        EquipMsg equipMsg = equipService.changeType(requestEquipMsgVO);
        if(!equipService.isRented(equipMsg)){
            equipMsg.setId(Integer.parseInt(requestEquipMsgVO.getId()));
            System.out.println(equipMsg.getId());
            equipService.deleteEquipMsg(equipMsg.getId());
            return ResultFactory.buildSuccessResult("删除成功");
        }else {
            return ResultFactory.buildFailResult("设备已被租用 不可删除");
        }
    }

    //删除设备类型
    @CrossOrigin
    @PostMapping(value = "/deleteEquipType")
    @ResponseBody
    public Result deleteEquipType(@RequestBody EquipType requestEquipType){
        System.out.println(requestEquipType.getId());
        if(equipService.isOccupied(requestEquipType)){
            System.out.println(equipService.findAllEquipVO());
            System.out.println(equipService.findEquipById(requestEquipType.getId()));
            return ResultFactory.buildFailResult("有设备占用，不可删除");
        }else{
            equipService.deleteEquipType(requestEquipType);
            return ResultFactory.buildSuccessResult("删除成功");
        }
    }

    //开机
    @CrossOrigin
    @PostMapping(value = "/openEquip")
    @ResponseBody
    public Result openEquip(@RequestBody EquipMsgVO requestEquipMsgVO){
        EquipMsg equipMsg = equipService.changeType(requestEquipMsgVO);
        //修改的时候需要传入id
        equipMsg.setId(Integer.parseInt(requestEquipMsgVO.getId()));
       equipService.openEquip(equipMsg);
       return ResultFactory.buildSuccessResult("开机成功");
    }

    //关机
    @CrossOrigin
    @PostMapping(value = "/closeEquip")
    @ResponseBody
    public Result closeEquip(@RequestBody EquipMsgVO requestEquipMsgVO){
        EquipMsg equipMsg = equipService.changeType(requestEquipMsgVO);
        //修改的时候需要传入id
        equipMsg.setId(Integer.parseInt(requestEquipMsgVO.getId()));
        equipService.closeEquip(equipMsg);
        return ResultFactory.buildSuccessResult("关机成功");
    }

}
